package service.impl;

import mapper.AppInfoMapper;
import mapper.VersionMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pojo.AppInfo;
import pojo.AppVersion;
import service.AppInfoService;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;

@Service
@Transactional(rollbackFor = {Exception.class})
public class AppInfoServiceImpl implements AppInfoService {
    @Resource
    private AppInfoMapper appInfoMapper;

    @Resource
    private VersionMapper versionMapper;

    @Override
    public List<AppInfo> selList(String softwareName, Integer status, Integer categoryLevel1, Integer categoryLevel2, Integer categoryLevel3, Integer flatformId) {
        return appInfoMapper.selList(softwareName,status, categoryLevel1,categoryLevel2,categoryLevel3,flatformId);
    }

    @Override
    public AppInfo view(Integer id) {
        return appInfoMapper.view(id);
    }

    @Override
    public AppInfo apkExist(String APKName) {
        return appInfoMapper.apkExist(APKName);
    }

    @Override
    public AppInfo nameExist(String softwareName) {
        return appInfoMapper.nameExist(softwareName);
    }

    @Override
    public int add(AppInfo appInfo) {
        int result = 0;
        try{
            if(appInfoMapper.add(appInfo) > 0){
                result = 1;
            }
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    @Override
    public int update(AppInfo appInfo) {
        int result = 0;
        try{
            if(appInfoMapper.update(appInfo) > 0){
                result = 1;
            }
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    @Override
    public int del(Integer id) {
        int result = 0;
        List<AppVersion> appVersionList = versionMapper.selList(id);
        AppInfo appInfo = appInfoMapper.view(id);
        if (appInfoMapper.delVersion(id)>0){
            try {
                for (int i = 0;i < appVersionList.size(); i++){
                    File file = new File(appVersionList.get(i).getApkLocPath());
                    file.delete();
                }
            }catch (Exception e){
                e.printStackTrace();
                throw e;
            }

            result = 1;
        }
        if (appInfoMapper.delAppInfo(id)>0) {
            try {
                File file = new File(appInfo.getLogoLocPath());
                file.delete();
            } catch (Exception e) {
                e.printStackTrace();
                throw e;
            }
            result = 1;
        }
        return result;
    }

    @Override
    public int onSale(Integer status, Integer id) {
        int result = 0;
        try{
            if(appInfoMapper.onSale(status,id) > 0){
                result = 1;
            }
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
        return result;
    }
}
